package tx.oopa.circuit.view;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JComponent;

import tx.oopa.circuit.node.AbstractNode;
import tx.oopa.circuit.node.EndingNode;
import tx.oopa.circuit.node.StartingNode;

class NodeComponent extends JComponent {
    private static final int STROKE_THICKNESS = 2;

    private static final int WIDTH = 50;
    private static final int HEIGHT = 50;

    public static final int ACTUAL_HEIGHT = HEIGHT - 5 * STROKE_THICKNESS - 10;
    public static final int ACTUAL_WIDTH = ACTUAL_HEIGHT;

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private AbstractNode node;

    public NodeComponent(AbstractNode node) {
        if(node == null) {
            throw new IllegalArgumentException(
                    "Node to display cannot be null.");
        }
        this.node = node;
        this.setSize(WIDTH, HEIGHT);
    }

    /**
     * Gets the node displayed by this component.
     * 
     * @return The displayed node.
     */
    AbstractNode getNode() {
        return this.node;
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        // Initialize graphics object
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setStroke(new BasicStroke(STROKE_THICKNESS, BasicStroke.CAP_ROUND,
                BasicStroke.JOIN_ROUND));

        if(this.node instanceof StartingNode || this.node instanceof EndingNode) {
            g2.setColor(Color.BLACK);
        }
        else {
            g2.setColor(Color.WHITE);
        }
        g2.fillOval(this.getX(), this.getY(), ACTUAL_WIDTH, ACTUAL_HEIGHT);

        g2.setColor(Color.BLACK);
        g2.drawOval(this.getX(), this.getY(), ACTUAL_WIDTH, ACTUAL_HEIGHT);

        // Draw name under node
        Font nameFont = new Font(Font.SANS_SERIF, Font.BOLD, 10);
        g2.setFont(nameFont);
        g2.drawString(this.node.getName(), this.getX(), this.getY() + HEIGHT);
    }

    private String getNodeContent() {
        String content = "";

        return this.node.getName();
    }

}